import Vue from 'vue'
import VueRouter from 'vue-router'
import Admin from '@/views/Admin'
import { isLogin } from '@utils'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/admin'
  },
  {
    path: '/admin',
    name: 'Admin',
    meta: {
      needLogin: true
    },
    component: Admin,
    // 配置二级路由
    redirect: '/admin/dashBoard',
    children: [
      {
        path: '/admin/dashBoard',
        name: '仪表盘',
        component: () => import('@/views/DashBoard'),
        meta: {
          isNav: true,
          icon: 'dashboard',
          needLogin: true,
          roles: [
            'admin',
            'superadmin',
            'ddd'
          ]
        }
      },
      {
        path: '/admin/itemList',
        name: '商品列表',
        component: () => import('@/views/ItemList'),
        meta: {
          isNav: true,
          icon: 'book',
          needLogin: true,
          roles: [
            'admin',
            'superadmin',
            'ddd'
          ]
        }
      },
      {
        path: '/admin/itemAdd',
        name: '增加商品',
        component: () => import('@/views/ItemAdd'),
        meta: {
          isNav: false,
          needLogin: true,
          roles: [
            'admin',
            'superadmin',
            'ddd'
          ]
        }
      },
      {
        path: '/admin/itemEdit',
        name: '更新商品',
        component: () => import('@/views/ItemEdit'),
        meta: {
          isNav: false,
          needLogin: true,
          roles: [
            'admin',
            'superadmin',
            'ddd'
          ]
        }
      },
      {
        path: '/admin/msgList',
        name: '消息列表',
        component: () => import('@/views/MsgList'),
        meta: {
          isNav: true,
          icon: 'mail',
          needLogin: true,
          roles: [
            'admin',
            'superadmin',
            'ddd'
          ]
        }
      },
      {
        path: '/admin/setting',
        name: '设置',
        component: () => import('@/views/Setting'),
        meta: {
          isNav: true,
          icon: 'setting',
          needLogin: true,
          roles: [
            'superadmin',
            'ddd'
          ]
        }
      }
    ]
  },
  {
    path: '/login',
    name: '登录页',
    component: () => import('@/views/Login')
  },
  {
    path: '/noPermission',
    name: '没有权限',
    component: () => import('@/views/NoPermission')
  },
  {
    path: '*',
    name: '404',
    component: () => import('@/views/NotFound')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由首位做登录鉴权
router.beforeEach((to, from, next) => {
// 判断那些路由需要登录
  if (to.meta.needLogin) {
    // 判断是否登录
    if (isLogin()) {
      // 判断当前用户角色能否访问当前路由
      if (to.meta.roles.includes(JSON.parse(localStorage.getItem('userInfo')).role)) {
        // 可以访问
        next()
      } else {
        next({
          path: '/noPermission'
        })
      }
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})
export default router
